//
//  ViewController.m
//  3.17动画
//
//  Created by student on 16/3/17.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,assign) CGPoint p;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *arr = @[[UIImage imageNamed:@"chara1"],[UIImage imageNamed:@"chara2"]];
    self.imageView.animationImages = arr;
    self.imageView.animationDuration = 0.5;
    [self.imageView startAnimating];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint p = [touch locationInView:self.view];
    self.p = p;
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(move:) userInfo:nil repeats:YES];
}

- (void)move:(NSTimer *)t
{
    CGPoint center = self.imageView.center;
    if (center.x != self.p.x) {
        
        if (center.x > self.p.x) {
            center.x -= 10;
        }
        if (center.x < self.p.x) {
            center.x += 10;
        }
        if (center.y > self.p.y) {
            center.y -= 10;
        }
        if (center.y < self.p.y) {
            center.y += 10;
        }
    }
    else
    {
        [t invalidate];
    }
    self.imageView.center = center;

    
    
}


@end
